{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Modeling and Simulation in Python\n",
    "\n",
    "SymPy code for Chapter 16\n",
    "\n",
    "Copyright 2017 Allen Downey\n",
    "\n",
    "License: [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Mixing liquids"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can figure out the final temperature of a mixture by setting the total heat flow to zero and then solving for $T$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *\n",
    "\n",
    "init_printing() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAREAAAAUCAYAAACqLgmJAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGYklEQVR4Ae2cj1XcOBDGd3kpgCMVhOuAJBUcdEBeKgh0cHlUwCMdJB1ckg6ACgh0EDoIlw647yekPdmWLcl/1uza855YWRpJM6NPo5G8y/Lx8XExJi2Xy32Nfyg5vowpx5hjywbHGv9ONrgfU46xx56xsFhsIhaWISciRXYFqBOlI6XfSg9Ke0qXLHY72afKf1RZa7LjfFM/jGPI9v1TD4z7w35Sx0KDrpSog3BAB0qfuspCZyk0lHzq95vG/yg9npUjsXM0YyEAjilhQbpeyAS/lFh7fyr9I6ze6XOxUKaQVPS30r9KJ4E6FvLnuvoyf+xZ/dwq7ft8ekbYC7+MvAiZfgbKkacia5mvr2eNNYh86ndX6bYvOfvoR/LMWCitD9+uU8ECuFQ6drorD1bZ6M3a3VHGEDuO0qUeTpVeqUHleKGy70/cphMigtaksdjdfqjP8s6LYIUIR7xEHAgeGhMFy32oaDAaRD7pjIe/kq4s3Nak9sc99DFjIW0GnjUW0lRo5hKWWKfo6dY+QQdY5ZnoebFyIspfK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5CCEsM9DCEfdhlF376wYDecK/VXuyl5UzJJLCTaaDQsuPnRHPK9rQf8gCtTnsgEX/GBMiIRvvT1XRV/KIsjYdd3zoJQkx/IuTcweuyFGMO/hFt1qvHxenhgkguJr1WOs7tJBKZYh6E1yPdGkpvJGUaD5l67YkH2AVxcvqdiZnJYyLDRqFjwkIIzO5PcrEnordJfmmM2gEXwV7xU5JA659jB673yubi2G7XhHfiR2uActp5SbCQePDw/wku2Y9lw6oMdo+3Farm7rGerI3NqQmv7/KBnA7a6zsQ3GSyk2kh8nbFQZ+++y3d66hCFc8mcB3MbbTB/io3OpF8wQsvQm6NQ6KcDGV3kswr0RI3M6Y3yHIFxZjiTlE1iEljItFEfWJD5h6dOkYiMQngDUIhEyPPmJvnIofaEsvxfkhSgiXXzKNVG4sPJXMsWKXdFz84Qkp9/1VBxlNLHvCOMCTwRLCTZaNOw0MmJxIARq7fGKvxTolibba2fwiJqmrsZC/9bZ9OwMKoTwWwyGBHM1P89IpeRvM3Y2oiMuY7RjAWzHjYOC/8B+/srpaXlJckAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle C_{1} \\left(T - T_{1}\\right) + C_{2} \\left(T - T_{2}\\right) = 0$"
      ],
      "text/plain": [
       "C₁⋅(T - T₁) + C₂⋅(T - T₂) = 0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C1, C2, T1, T2, T = symbols('C1 C2 T1 T2 T')\n",
    "\n",
    "eq = Eq(C1 * (T - T1) + C2 * (T - T2), 0)\n",
    "eq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIUAAAAyCAYAAAByFRjxAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAG5klEQVR4Ae2di3HcNhCGTxoXoKiDKB3YTgWxO4jHFcTqwC5BI3cgp4PIHciqILY7sNKBrA6U/+NhOR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ilkBBZsbC8ZKSd6Z2vrMl/ssCNdIEYNfARDaWG9jxMREuOf7FKIcGxZ7F0l8Pf+5hdMKVgpNnGCURu/9phdl3VP0Hqc6yU7ciCINsvslvnZb88y3+Z0L9YDhuilIJ9kco/gfkfdpsalzALf7AAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\left[ \\frac{C_{1} T_{1} + C_{2} T_{2}}{C_{1} + C_{2}}\\right]$"
      ],
      "text/plain": [
       "⎡C₁⋅T₁ + C₂⋅T₂⎤\n",
       "⎢─────────────⎥\n",
       "⎣   C₁ + C₂   ⎦"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solve(eq, T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use SymPy to solve the cooling differential equation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPwAAAArCAYAAABckOXwAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAJXElEQVR4Ae2di5XUNhSG2T1TQEIJ0MGyVBDoAEIFkA7gUAEHOkioIEAHkAp4dACpgLAdbP5PKxlbI9uSrJnxwNU5Wo+kq9d/dR+SNbMnl5eX13LDycnJmWifK54rvlLdP3LrGp0hYAgcHoGTEoEPw5XgoyXuq+6bkGdPQ8AQWD8Cp6VDlLDf8XXeldY1ekPAEDgsAsUCr+HeVfwi635x2KFb74aAIVCKQI3AY+HNupcibfSGwAoQ2EyNQe77DZU/UcSaf1VE0Dm4e6ZowRAwBI4MgVGB93v115rPLbnvX5iX8t76+ZmF90DYwxA4JgSSLr0E+xdNAmF/EoTdTwrB/2T7d4+GPQyBI0MgKfCaA+/aEfpX0Xxs/x4BYklD4JgQGBP43zWJd31L7q0+e/rg1h/TPG2shoAhIAS2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KId/EHL4+qFCw05XUAfDlBy6H90mhoMW2PCGJ5r3SCgLmjt9HlEfnzwiocG39ma9cs4oHuqtt7reaGyQ1pnDaFJqOFRzjof0Mz+4o1Axd1Ag97sT0v5MONc+WMn6X3yRZ/9ImGPNstY0XIoxE9s71oRLZrTviuXYLjvsVl/VwiU8ChnnadocgSe072tL4ioMdytNxKs2j228dkQMAT2jMDApfcaBgG+UPyqiBvPYckzxdKvplLFgiFgCKwIgc7CS9jZq+Pvx/88kvxfZclRAi6Ilv07By47v5bru7SHIWAINEDglDYkwJzkIey5/zxycLWVNiwYAobA+hFwAq9hcjCH0MdfbsG6D07nlSaM5V+V2l9DwBBYJQJB4PliRtY/j/TeAK9Q+hcfVjk5G5QhYAgMETj1Aox1j79phhXnJkRs4c/jfN8G2RYMAUNgxQgEC88Q30fjfKC0UwIS6P6/bxrs3ykTHRbfgiFgCKwcgVN/+o4V74RWQszlFV7Hhcsr/X8eiTfg8r1l57Zd7B2IxIIhYAisDQH3Ws4L7ksNLlh5FMCFIod55PHDic61Fy2KgVt2nOrj8s/efoPOgiFgCBwegf8Be2hi0A/WnWEAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\frac{d}{d t} T{\\left(t \\right)} = - r \\left(- T_{env} + T{\\left(t \\right)}\\right)$"
      ],
      "text/plain": [
       "d                           \n",
       "──(T(t)) = -r⋅(-Tₑₙᵥ + T(t))\n",
       "dt                          "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T_init, T_env, r, t = symbols('T_init T_env r t')\n",
    "T = Function('T')\n",
    "\n",
    "eqn = Eq(diff(T(t), t), -r * (T(t) - T_env))\n",
    "eqn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's the general solution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMAAA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4r2TY5GYs0LuH1uafmFt78VgMrYiOzvyUwWjcLDfuU1bXihWJmVPd1mcAnq1YdAhL8AgGoDPGdNQuNMuiuwwSPKWLOOiZRS0Z/Da+Il5CJAh1wZzcGaKEzMpxwmYYANwAN8Z0W1I+graGlAaCbRhYjd4VYCd1Oc64utbe4nlCJ/H/ld9pZuHaUSMu/R5+6/Nd9lsgkHpDYNg62XqrCJjT22mKCK+R1hoo0MCoDuB6GLTRz1qr9r4cH+B6MD4Htzhj5Git+9caGKKB/wDq6n1MAu/apAAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle T{\\left(t \\right)} = C_{1} e^{- r t} + T_{env}$"
      ],
      "text/plain": [
       "           -r⋅t       \n",
       "T(t) = C₁⋅ℯ     + Tₑₙᵥ"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution_eq = dsolve(eqn)\n",
    "solution_eq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAH0AAAAWCAYAAADts5O8AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAE0ElEQVRoBe2Zi1EbMRBAbYYCgBKgAwgdkA4gHQQ6gKGCDHQAqSBAB4QKCHQAHQTcAXlPSJfznXycwT8MOyPrpF2t9qNdfdx9enrqzDt0u90bdPyOrrfzrmsb/brz7nQcvoQhHtGz28YgH4Fm4QMouYWO9x9Az9YqvutIJ4p30fQr5ZLyQFmlbFIOKIK1Tu9R/lBOPlN8pzMPTjeKdfpXHPqbhXDH957f1J24n/+gfWH7EwY4Pe6DKYqMEqNohXKJ8U7BG1EaNkUUzclClEG5gpzIYsTXADpPqmvgp5Lio5wuxJRtrIXt56rj4kx92nWdcjxO2y7GiYsKIfdpHFIOqoYEt005AfdNPGVkAF/TcBueO8jVS05knOncSK8BOA1Y0NYIJtOxxzQ1JyLbI/0PGRtrXxfJ+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rAxyjAdD3PYNTlnlPHobNzpYxghtS2P9NEtEOKZwGV/28w6C7BuTirYLaWj+NCplisUgzTjoy8fzvpKm33olzKGobtzNFi4LTdTUI2T9puszopAPOX3wHsL1J2JDHSTeXu82Wc2/IhvK6pTf9h4f4D66qLhYPzB8MAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle C_{1} e^{- r t} + T_{env}$"
      ],
      "text/plain": [
       "    -r⋅t       \n",
       "C₁⋅ℯ     + Tₑₙᵥ"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "general = solution_eq.rhs\n",
    "general"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use the initial condition to solve for $C_1$.  First we evaluate the general solution at $t=0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFcAAAARCAYAAACoyTAdAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADlklEQVRYCc2XgVUbMQxAG14GADZo2ADKBnQD6AhhA3hM0AcbQDdo2CBlAlo2gA1K2YD+79j3nDuTO8gFovd0d5JlWZIlnT14fn7+VIfBYLAJbwx+BZ/AR3AbnCJ/xfiI72O+T3mvBUSb7jFGe3/Ht7Yd+gB+gY4J2r8LXqzUB4ObIwuegP/Acc73G9DQS7A4Xpd/TxqbzsX6mtHW+wJfPxo+1uWWoYcsECBm6wTCXf2M0rTLMwGe8K6RM5s3QTOhV0C3mzdinYs3KHbeUT4vZrO2/sz58fsP74cCvzfWRqbphu8v4F4psJmcG/CEzEoNy9Zr/SSIlvi0IHgQeaUxg277WBmEzMU4S0QDj1oCqyH231ImOPZhgN1XhcWtMqFUZXcdfJ3NfuNzwDzbgD+CBxbbadPDRrjj28j2nrnoXqYtNExHn/+Gxy5+NSb3wLAtHEc9Zm8ruNv1wOLELlgqvVZ9qxLAHpNmJf+GrjbbFswWoVQ6s5EXngaVobQ5OrNOsKjfvoudtgVLxx3eMivbViWgm3U5eDpyCb+1ragfeY9NyXlZCULLgSi1HNvW3GkgTSq9WcMfr4nTya+SjmV5Zq4/qEbASooxWGN1/K403pVHkIqXj6j/rUex+vJunhvSmjD1iX3RBtd2MMYxnSplTL7WPjLXOWMdv/UFu6yChaca5JQ5A/+CAfAvnLEZ84a6B3oe9siWKm0HmdQKrUL5tkdjEyoLnpUZQEO8fp0wOHdby2nGnWCGN2Tgu0DjFlSSXcRDh5Wx0I5F89MYOgyMPh0mXv3NmIE1cMGnSE+SHLQ69GvuNhppEzHEATrcCnm7XtKlH9MhQg9E2ohPeD9Bz50X4SnoefGUsQ8rMdZ/DaTevOgn/QOFVuo3fAy68S9lngln1htgj3J5TNwUW6lZq9x33m5C1YKQ9ya7P1QoElt8nsP0SJWCqBJ3syoD6LUE7LaydFa0TIUb+AbwFh9CuQfu7GFAvDQ1NgBeaI8GCJnqiArtHBMwxCeTM/mqdhnlpo0SZ8KreSh20bVpC118wF7LuCpv5wChrNN8aFvCbkYb6NQGcr6XsINMLrS2DZh9gKXSB5hFeQn2ofMlHa6VMtwS1wdbRQBoK8Bg5ycjk2jKmK2yDnmrCJk/rEu8ho4GeKxy0RG0Z8tSCXZSiyOpHXWSX1LI/mobNIgBWD/1amn5VakHgVmLsAVYpfmYbfMMXbe8q//Wf7XWnjUk+QRkAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle C_{1} + T_{env}$"
      ],
      "text/plain": [
       "C₁ + Tₑₙᵥ"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "at0 = general.subs(t, 0)\n",
    "at0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we set $T(0) = T_{init}$ and solve for $C_1$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAA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/ZDk+UHbxYI219Fs77Cx2uDKziueGeeyv6MStAOKRtj0yuaK/mUPoPQhFsRFd/j7G1SdWur22v9O6ZN8Yn3a1RvDvuz/MjxRH1RyTvAjzfFr1BR2edqjDCopjZXYQAUqVM6/Sq/B66+k4Jzdn6iuH/VQYi04DXKFFX30R40VH5wcjsxj9E9bkGd7G/hdwEdUPTJGXKv/gEph4EyR8bG1gAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle - T_{env} + T_{init}$"
      ],
      "text/plain": [
       "-Tₑₙᵥ + Tᵢₙᵢₜ"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solutions = solve(Eq(at0, T_init), C1)\n",
    "value_of_C1 = solutions[0]\n",
    "value_of_C1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we plug the result into the general solution to get the particular solution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAAYCAYAAAAia7s5AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGyUlEQVR4Ae2ci3HcNhCG7zQqQKN0IHfgWB04HchJBXI6kEcVaKwOlFSQWB1IqsCWOrA6iH0dKP+3AjggD+Dxcccj77AzEA+PBRY/d7ELgPb85eVllikjkBGYNgLz+fxRMziXPT/Ns1FP+2Vm6TMCMugjofBTtjwHjYMMSUYgIzB5BN5rBs9+FtlTeyTyMyMwQgTkhT9KrN+U7pR+KJ0onSp9UoJ4YtQLpW9KN9lTC4UYCcwTB2isOpftOAJ692fowEimeSM5SAuF2Nd6vlU60e9npT8pV7rit9LrntoJ/91VYu00gs5eH7N7PX0ZE6XTa3XgVwvXbDcewuNIM/mi+bFCro0yzrPZlDCQrF/08j9JD4rQdm3K0KAjhxXe2bx1Sh/VjtPuN17OQ9c31r5kpGr8U+U/qp2pnFWDRWBX6UET+7CByW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k66WO2nTf5Qg1bJhxB1ISh7CFam37XKGb8GNT7lWQXx8gDBJ6bhHTiLAd7QfwN8pfa8GPa6tvfQ81Zlp7TZArFSr+WTwAay7zPOHp5aDJwuoSMsIoWOuPI2Olbi94PrSZSY8s5BM9OJOnvA05fOcCQjuk7UaZFn2Nmmf5tRSwAGxhhJTBR6UDmG+1WGZmG1lb7+wRCje0+1tTBEvBhmcQWhPDx8o+qN17fjoORWyci129apO+MWMjuR1vbQ3PYe5zYYBLrEtVDxXoLypjpW4ueFSg70HH00PaSsht6p7jxVrz6u1R99IiPe30jlg19n+YGjIYUESpaLkfinCKNpK7Lw2fMpT6jNyaL1o99M2IdSYTmhy/ugXa9wRX31Cg3FjzyluXnZhn5KjlHi3BfjNjhqLDweOPAs9IY+RCt1zPEt8aucU2rbMro20Xfu6r63kXnbbQ8kdBciFPEenRUKwAnJjZTH42PkYWiDp75THUZXpTAkb7JyVvnDPLKFIX9Y1+Q33nQbh3Qx2caKc1+MY3NNlaE3/rPkIsRtoWNRfvW59M10QoBLlRdRQqLNqIqjV1qrJHRGjPLj1YxkwEWIrnqA5B9s+z0Ths/K+IsSq15hdL6tyvkonXCoqFN+KySZCMORv+8C00t+ycFiubM4NwHHYYDD4F1wQm3vxOuN8rU6VsOPU8Jg0bt79RM6IBVZiA7+D6ob6pzFxu37p5NR9x107PxOEdb+nySMfd5ZvjIC0oNRLO5lqVbnslEnMNILZQvBXn/rkUNCxFy8QQT0/rm9wYNvNVrrMsX/AQ7ur2h3Y03kAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle T_{env} + \\left(- T_{env} + T_{init}\\right) e^{- r t}$"
      ],
      "text/plain": [
       "                        -r⋅t\n",
       "Tₑₙᵥ + (-Tₑₙᵥ + Tᵢₙᵢₜ)⋅ℯ    "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "particular = general.subs(C1, value_of_C1)\n",
    "particular"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use a similar process to estimate $r$ based on the observation $T(t_{end}) = T_{end}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "t_end, T_end = symbols('t_end T_end')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's the particular solution evaluated at $t_{end}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQ0AAAAYCAYAAADgfqiUAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAHq0lEQVR4Ae2ci5ETORCGvdQGQHEZQAZwmwFkAHcRABlAbQQUZMBdBBxkwG4EPDKAi+BgM+C+Tyup5LHGHns8tsc7XaXVqPVq/Wq1Wprxnvz69Ws20YTAhMC4ETg5OfnCCJ6ynr8OPZKTyWgMDfHU/oTAsAhgMG7Tw0/W8smwPV23fmsXnUx9TAhMCAyKwENa/z5oD0Xjk6dRgDE9TggcGgJ4Ec+Q6RHhI+EH4S7hjPCSIBlrNK4Inwlvhz6iTJ4GKNeIybobJ6yWPfGOHAHm/rE6cCDDfIschisMwhvi+4S7PH8nPJdPeOUzYZA7DbB4S/hGP7PgaURwZCRrZSw9vo5mF8SJJ5AK/QYBk7WLxY4jAo/bjOQ949PCb40mnFG464U4Cl1D1vdM/kv0YGeuf6lsESu9i+BttOkj5XybcW9IOelDb+YJfTw/jUJqrRaMAAV/wv/RFBa+Vi9YnVj/2KJLBvRkgEFNOM9mY8LgKTqgLjzYhi7Ehddlo3Vx6lUEY0U9jyMeTxaIPDfwXHahwPYYbqC+oZklo6GrM7dIEEaPwh33Hws2yMp7sb4NORaSyK135Hh049Ym6mvVP6cJW7uB5RWOAueeGI8GA3TgirFeEF5sqk+lOtCGHrthXXKXf9VSybzQJnK6Xu8k3SWtLt8jhA0e/l886+3J1xBqiO4QJD2VOYMWy9mm5LoK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xWNAR24h1N9KxJANtqMOtel6Uc670NKo6RZ6XhUHOZhn4G+lis51DSp8yqCYlF2uZi+4ZTsv6B1Y61GdQoR5prbhvXQTaT9DLb0A0NuGiMZZ7RezEe9cQzn7EH+CdwdsHeW7eyifDHYS/yTgneJZiUOiIRirrSOSvo2Nz9VPnxHq5Xe5K1Ill60FPZe4ODRnVdb3m4DkTHw0Fo8EAHZiL3SCQ0iV8DcMnFnI4dgTu9R8XevXsT9ngplHXhZ9vjklb56owDqmcF2H55jaW29dbG/vNMvO8VWJsNx7ndTAodMm3AHleCn5XHZur76Qih3quPgY9lLeEfifvaVs+bbyhPdtUxvyKE3717UVbO6PhM7Cqy7WMz+D0D/Mxw7JQOF6keqQ9ingznY4iAppczZKva/ewKNfLnaOtXq4z9ZVnbmxJtl3HyHGQOPfFeB0c6csdWxyMs97YBrRSx2K9hfrwfcsRjtSxTHXOY54fQq29To61zi1A2YR01ZJHooV1Qj2yBCKtx6IRKV0/PY2P5Lmom1QeWbpY/mb9Mq1s5ZGozOvyrDewj0vYmmyHinNfjGtjbeOpN+lnC/kIsIaOVevTpm9Myt9stOndOeWyl8PzjafqK9dVqEQj4eJyVw6EgchHGPKdKD9hTWdWDYuW/TeCVjsv6lQWvhOou5jzSO+FkMljSv4Edy9C0ClypHPxUeLcBdeIgRuSi9o3HGFxJ71ZpWNL6rvpaRDUuwvaKTc4WBn/S/J2dc8V+j30PxsZjUMfVF/5oqJt/Z/w9JVrqr9bBNCDg9g8djvq1b1NRqMFIxTGI5Z3LXv3fFpEnNgDIsD8+/ZPD6Tt2DJg74fd9P9VXbGAjnyP0QAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle T_{env} + \\left(- T_{env} + T_{init}\\right) e^{- r t_{end}}$"
      ],
      "text/plain": [
       "                        -r⋅t_end\n",
       "Tₑₙᵥ + (-Tₑₙᵥ + Tᵢₙᵢₜ)⋅ℯ        "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "at_end = particular.subs(t, t_end)\n",
    "at_end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we set $T(t_{end}) = T_{end}$ and solve for $r$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAJ4AAAA/CAYAAAARxXEwAAAACXBIWXMAAA7EAAAOxAGVKw4bAAALNklEQVR4Ae2dj3XUOBDGSV4K4O4qOOgAQgVAB5BUAHQAjwp40AGkggM6gA7g0gFcBYR0wH0/RaPYXsuW17LXizXvaW3rz2g0+jwaybL34NevXzeWTgcHB0+RUbK+W7qsa5VPfXRTbT9THz1O0cFRSqZd5lGDnqv+UzXo7i7lKHV3a0D9c6m+eqvwb0pfHShTN8cdpqoRD1T9B4W/adgORSlVJ2rAG4p76q9Oy7dY4KkBmO7/FO6rEeeJ7S7ZFqABrJ7EeKt+i7pGSwYelg6/rvPOGapnKQV/8bXCZ4ULhVsKWNaP/vpPHR8pPOtSnNJnp32RXXKiU8AXH6mkXDp3UUEC31HAB7iVWzbxBNA3ja/OAaIur3Xg4x5U45ZwLrn2RnYv64eY3g6VYYl0JqE+SujvOYXTncjw/UV8q/4ikxasX5WwhFnrrjKPnUs+Z+Xb0pcue4vMrxT3yMu9kbw44ElQzDQW78WGtOMj4N30Oxhmmz7kZW7QJ4rOjRGjpctek1v6Q6cEtxRWS+RCGRYVJNJbBabkk8uleuhohvRFDKuS41Nqu5cme5vckhHQ/WxLW5zFk6AnCv8ozEHHvpKvc1SWuY5e2TV64OBvRWPKVirEhbkpXowqNTpU5B2FD1SkwGxvZ6T6GU6wQk2fayqZHorxd92RVZ9vqrpy802R/f6ISjfK0j8KUT+0WZf0ip+MbpG1RkdKPBezJ4plzYyO3yW5OwOZUoSQ3ORP8QUfR8CFL5lUV4o8Q/L4Dmzqm45tWqnYeliv7JE2J4kZKYu+vyQxuM6EEdm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X3I0A3rYuGt3bH6yJ4+dSpgL4DX2YKSuJcaWLqPt5dKLUL3a4Ddm24FsT9ryVE0kE8DZajNp8vCaYAGylA7QFklaz4NFODl02XhNEADBXgDlFWy5tNAAV4+XRZOAzRQgDdAWSVrPg0U4HldauX9m39Ckk+7hVNUAwV4Uo0Ax/Z1NoN2PtiOarEkDNZAAd6VyngzrDwHHgyf7QsU4F3pjvcbkh5ub6/qUrKqgdU+udDwCtjY73dLwYDHUMs7FmXjqRQxJa0WeKZU79+xhyz1y5ZWdOuj6nTvOIgBu3RXOcQX4F2BgH1ks73bIeCxU5c3sGYD+9Z3yUQFi493PcxOpOJWtgztq7R0po1VA89bHnw8tmzPSXwyYvIvV83ZoKF1HQ0t8JvlP6Y9GvLCjBYw6vqSeA9M3g/9wTWkNDfxUBqvXzI880YVIMKKQXwnhHddA3k+fK3A3r0l76uQYY0nUtIivyYwh1zqb8AQvligcxaS7b878MOqb9hzHT5PoXP3RQEd+UxD+G8Of137MoHiAJzxtW+huDf352jnEutYu8UDTO5phbdKAMY+MHnm0/gGtE6dtWP5xV4+eq9TwHehMtW3quB5oWAEuD8rT9Wnu9T1pWVY43HtwAMUfMETAIVh1AOB4ZDv1YVh2MeTz8CKr8bSiCPxoUwTVCeKe3KVw/2SZ4NnJX0Vp6sGngcQj8vaKFhDS8QqNiwVIKr6alhEZ/2UlyEVgMKnau2ojz/cIx4Qr9LyrXpWS8d3EFYpfMHAA4Xh15GumQ0DxCqoACJ/MIivaKAKwFI8/MjDZORkraBT22+s2uKhgA7CevHfHQDMkYDifDx/Sbz5gz7KDbtYNB67WZrx4fvOWEC+oMSsl4nLaul/jEX3SDiBw5IAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\frac{\\log{\\left(\\frac{- T_{env} + T_{init}}{T_{end} - T_{env}} \\right)}}{t_{end}}$"
      ],
      "text/plain": [
       "   ⎛-Tₑₙᵥ + Tᵢₙᵢₜ⎞\n",
       "log⎜─────────────⎟\n",
       "   ⎝ T_end - Tₑₙᵥ⎠\n",
       "──────────────────\n",
       "      t_end       "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solutions = solve(Eq(at_end, T_end), r)\n",
    "value_of_r = solutions[0]\n",
    "value_of_r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use `evalf` to plug in numbers for the symbols.  The result is a SymPy float, which we have to convert to a Python float."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sympy.core.numbers.Float"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "subs = dict(t_end=30, T_end=70, T_init=90, T_env=22)\n",
    "r_coffee2 = value_of_r.evalf(subs=subs)\n",
    "type(r_coffee2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMoAAAAPCAYAAAClQFCvAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAHXUlEQVRoBe2Z7XEUORCG1y4HwHEZLBn4uAwgg6OIAMgAyhFQkAG+CDiTgZ3BHc7AvghsNgPf+8yqVZJG0rR2jn/uKq00UvfbH2pppNmjh4eHjdHR0dEnte9UdirPVL5q/Fq1i9bKu5Q0mBLdcPyqMmR7CZvgNWMhnieSI2bQdl9tPtRi5uUVHzjvAhbVqcoXYX5L+lY1pQPMd8JM9Wy8NqLcwxv0XIidHLpXIZYZyYYPdIjX7XfQ/TYAMdfMw4WwrkLfVInvuxpfVP7S2C7Yg8+Xes7iGTCZS7MRe5jLW9WbjRpTURPQP5JnlN+obK2vV4/Ko0vlh8qTBdwuH/Iq2P7CcNQmON/tuaw1toS5GAth2OSkMSTQ7DzRFnR7ecXH5LAoUkxsBeRt2p+2Ndb1J+WlLWJeSaxUz8/wh2RGYatMNmh8yG/xZz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BtaqR3iFZwl+IbneAq6qCxP+tTrY6Ryx0cnL0Y+d30Utv6ULnNca575FvNlMSHRi44kR+cLdkrdXJOFMiw58FRZMmle3x2KwFWeLIgqrYROTCqXjfBJYJZ+BjT9gl+mvSWfBqDGkfYf6EgJLLDgudanHq7HZBAawf1QzF0NvFeEh87vscico+no2Mp7SAC+xqW5Eg35zfHuT2iD/2Ey4M8YYCZNFw4ZZkuXLLDeEw2ngHDwVYmY8tycBhVcMSkqyxWOvoHLcntfKG85ojTO95Gku8I6iIV80GRwDnimwcZEw8eCrL9Pv4OWYRaLyBcgmlK5Didiw+LKjhvo4qlg/n1TtrYXu/9Of1G5i0lqwLr9lGznKXWsWG/XxUYc7qeUsvmfx1zNkec7ms0TYzOLZMZkw8zqr/eHC6m1+v0f2UPmgE+Vr/kdhwmcY6iMxZv4U9s7kRn2RDvTP/itQH6/u+D9KwF3klQz/ZdT+IyCRqvYmNjfHzW+rg57ZfwzqX7QxwRjlxb7sj+EEy+13sD2LbYLDp+NprKOLWGY5recp/1XHXFQb/7B5j5coASD+I6w2Kw8H0j+G6EM4UwSGaFHedAV+kgnBaFw6bm2Nd/k0zleUmKzgqWB39MWwrNbYEuaiL8Jgt4GPzcQKCxR7yj++XLySY8Kyf531zA5PnLJ+84Va1PUn5Q38s8QUhsvGIO/mDfzmQ5yn1CZ8oxR9JlP286aY7qAFPxh8LLDNGxuzhYkOFXzPFpqeiV/EVJsc4jnqPgIYCq+1MzXvpg6dbVV/1Hj2uhQfyXGt/uzSOiBPMj1VwWEMAp/XIIsvfhERnotPcthOooK1UyFAM7vVB58L0+OLeAgku06N+HrDJXOiQV7iksYWf0iw2fHX68/eihgnbEYHxFGFN9j5oI1u31EibHxA5o10oXNG4hnxGx/I1XsV5py5n/13FvRy0be8gJ+viTxnJF5yCAILyvDiQtmPPf4+RuAxArUI/Aej+2yr5aP5QwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle 0.011610223142273859$"
      ],
      "text/plain": [
       "0.011610223142273859"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_coffee2 = float(r_coffee2)\n",
    "r_coffee2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
